<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryRef" v-show="showSearch" :inline="true" label-width="auto">
            <el-form-item label="运营商名称" prop="operatorName">
                <el-input
                    v-model="queryParams.operatorName"
                    placeholder="请输入运营商名称"
                    clearable
                    style="width: 240px"
                    @keyup.enter="handleQuery"
                />
            </el-form-item>
            <el-form-item label="运营商手机号码" prop="phone">
                <el-input
                    v-model="queryParams.phone"
                    placeholder="请输入运营商手机号码"
                    clearable
                    style="width: 240px"
                    @keyup.enter="handleQuery"
                />
            </el-form-item>
            <el-form-item label="平台类型" prop="platformType">
                <el-select
                    v-model="queryParams.platformType"
                    placeholder="平台类型"
                    clearable
                    style="width: 240px"
                >
                    <el-option
                        v-for="dict in dict.type.rule_platform_type"
                        :key="dict.value"
                        :label="dict.label"
                        :value="dict.value"
                    />
                </el-select>
            </el-form-item>
            <el-form-item label="联系人姓名" prop="username">
                <el-input
                    v-model="queryParams.username"
                    placeholder="请输入联系人姓名"
                    clearable
                    style="width: 240px"
                    @keyup.enter="handleQuery"
                />
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
                <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>
        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button
                    type="primary"
                    plain
                    icon="Plus"
                    @click="handleAdd"
                    v-hasPermi="['system:eaOperator:add']"
                >新增
                </el-button>
            </el-col>
            <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <!-- 表格数据 -->
        <el-table v-loading="loading" :data="list" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center"/>
            <el-table-column label="运营商名称" prop="operatorName" align="center" />
            <el-table-column label="运营商手机号码" prop="phone" :show-overflow-tooltip="true"/>
            <el-table-column label="平台类型" align="center" prop="platformType">
                <template slot-scope="{row}">
                    <dict-tag :options="dict.type.rule_platform_type" :value="row.platformType" />
                </template>
            </el-table-column>
            <el-table-column label="支付应用标识" prop="payAppKey" align="center" :show-overflow-tooltip="true"/>
            <el-table-column label="联系人姓名" prop="username" align="center" :show-overflow-tooltip="true"/>
            <el-table-column label="创建时间" prop="createTime" align="center" :show-overflow-tooltip="true"/>
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                <template slot-scope="{row}">
                    <el-button type="text" icon="el-icon-edit" @click="handleUpdate(row)"
                               v-hasPermi="['system:eaOperator:edit']"></el-button>
<!--                    <el-tooltip content="删除" placement="top">-->
<!--                        <el-button link type="primary" icon="Delete" @click="handleDelete(row)"-->
<!--                                   v-hasPermi="['system:eaOperator:remove']"></el-button>-->
<!--                    </el-tooltip>-->
                </template>
            </el-table-column>
        </el-table>

        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />

        <!-- 添加或修改运营商对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
            <el-form ref="formRef" :model="form" :rules="rules" label-width="auto">
                <el-form-item label="运营商名称" prop="operatorName">
                    <el-input v-model="form.operatorName" placeholder="请输入运营商名称"/>
                </el-form-item>
                <el-form-item label="手机号码" prop="phone">
                    <el-input v-model="form.phone" placeholder="请输入手机号码"/>
                </el-form-item>
                <el-form-item label="平台类型" prop="platformType">
                  <el-select
                      v-model="form.platformType"
                      placeholder="请选择平台类型"
                      clearable
                      multiple
                  >
                    <el-option
                        v-for="dict in dict.type.rule_platform_type"
                        :key="dict.value"
                        :label="dict.label"
                        :value="dict.value"
                    />
                  </el-select>
                </el-form-item>
                <!-- <el-form-item label="子商户ID" prop="subMchId">
                  <el-input v-model="form.subMchId" placeholder="请输入子商户ID"/>
                </el-form-item> -->
                <el-form-item label="支付应用标识" prop="payAppKey">
                  <el-select
                      v-model="form.payAppKey"
                      placeholder="请选择支付应用标识"
                      clearable
                  >
                    <el-option
                        v-for="(item,index) in payApplicatList"
                        :key="index"
                        :label="item.label"
                        :value="item.appKey"
                    />
                  </el-select>
                </el-form-item>
                <el-form-item label="联系人姓名" prop="username">
                    <el-input v-model="form.username" placeholder="请输入联系人姓名"/>
                </el-form-item>
            </el-form>
            <template #footer>
                <div class="dialog-footer">
                    <el-button type="primary" @click="submitForm">确 定</el-button>
                    <el-button @click="cancel">取 消</el-button>
                </div>
            </template>
        </el-dialog>
    </div>
</template>

<script name="Operator">
import * as api from "@/api/station/operator";
import {updateRule} from "@/api/station/billing_rules";
import {getAppConfig} from "@/api/station/operator";
import {setToken} from "@/utils/auth";
import { validateMobile } from '@/utils/check'
import { payAppOptionSelect } from "@/api/paymentManage/payApplicatInfor";
export default {
  name: 'Operator',
  dicts: ['sys_normal_disable','rule_platform_type'],
  components: {  },
  data() {
    return {
      form: {},// 表单参数
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      queryParams: {// 查询参数
        pageNum: 1,
        pageSize: 10,
        operatorName:null,
        phone:'',
        platformType:null,
        username:'',
      },
      // 总条数
      total: 0,
      rules: {
        operatorName: [{required: true, message: "运营商名称不能为空", trigger: "blur"}],
        phone: [
          { required: true, message: "手机号不能为空", trigger: "blur" },
          { validator: validateMobile.bind(this), trigger: 'blur' },
        ],
        platformType: [{required: true, message: "平台类型不能为空", trigger: "change"}],
        payAppKey: [{required: true, message: "支付应用标识不能为空", trigger: "blur"}],
        username: [{required: true, message: "联系人姓名不能为空", trigger: "blur"}],
      },
      list:[],//页面数据
      payApplicatList:[],//支付应用数据
    };
  },
  mounted(){
    this.getList()
  },
  methods: {
    async payAppOptionSelect(){
      let res = await payAppOptionSelect()
      let data = res.data || []
      this.payApplicatList = data
    },
    getList() {
      this.loading = true;
      api.listOperator(this.queryParams, this.dateRange).then(response => {
        this.list = response.rows || [];
        this.total = response.total || 0;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryRef");
      this.handleQuery();
    },
    handleDelete(row) {//删除
      const del_ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除编号为"' + del_ids + '"的数据项？').then(function() {
        return delOperator(del_ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    handleExport() {//导出
      this.download(api.EXPORT_URL, {
          ...this.queryParams
      }, `device_type_${new Date().getTime()}.xlsx`);
    },
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    // 表单重置
    reset() {
      this.form = {};
      this.resetForm("formRef");
    },
    handleAdd() {
      this.open = true;
      this.reset();
      this.title = "添加运营商";
      this.payAppOptionSelect()
    },
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      this.open = true;
      this.title = "修改运营商";
      api.getEaOperator(id).then(response => {
        let data = response.data || {}
        let platformType = data.platformType || ''
        this.form = {...data,password:'',platformType:platformType.split(',')};
        this.payAppOptionSelect()
      });
    },
    submitForm() {
      this.$refs["formRef"].validate(valid => {
        if (valid) {
          let forms = {...this.form}
          let platformType = forms.platformType || []
          forms.platformType = platformType.toString()
          if (this.form.id != undefined) {
            api.updateOperator(forms).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false;
              this.getList();
            });
          } else {
            api.addOperator(forms).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    cancel() {/** 取消按钮 */
      this.open = false;
      this.reset();
    },
  }
}
</script>
